﻿using System;
using System.Data;
using System.Collections;
using System.Collections.Specialized;
using WP.DB;
using WP.RM.DataEntity;
using WP.Commons;

namespace WP.RM.DataAccess
{
    public class RMSalaryDAL
    {
        string mstrFieldName = @"PrdYear,PrdMonth,WorkerID,WorkSalary,Remark,Posted,WorkDays,HolidayDays,STDSalary,HourlySalary,PrdSalary,RewardAndPenalties,OverSpend,AddAndReduce,OverTimeSalary,
                                 Subsidy,TotSalary_1,EndowmentIns,MedicalIns,UnEmployIns,TotSalary_2,PersonTax,WithHoldAndRemitTax,PRPSalary,
                                 TotSalary,PostedBy,PostedDate";
        public bool ConfirmSalary( DBAccess dbcon , DataRow drRow  )
        {
            drRow["Posted"] = true;
            drRow["PostedBy"] = SysVar.UserName;
            drRow["PostedDate"] = DateTime.Now.Date.ToShortDateString(); 
            DataFunctions.UpdateTable(dbcon, mstrFieldName, "rmWorkerSalary", drRow, FormStatus.AddNew);

            string strSQL = "Update rmWorkerworksalary Set Posted=1 Where PrdYear='{0}' And PrdMonth='{1}' And WorkerID='{2}'";
            strSQL = string.Format(strSQL, drRow["PrdYear"], drRow["PrdMonth"], drRow["WorkerID"]);
            dbcon.ExecuteSql(strSQL); 

            DataFunctions.AddLog(dbcon, "审核" + CvtFunc.ToString(drRow["WorkerID"]) + CvtFunc.ToString(drRow["PrdYear"]) + "/" + CvtFunc.ToString(drRow["PrdMonth"]), "RM", "");
            return true;
        }

        public bool UnConfirmSalary(DBAccess dbcon, string strPrdYear, string strPrdMonth,string strWorkerID)
        {
            string strSQL = "Update rmWorkerSTDSalary Set Posted=0 Where PrdYear='{0}' and PrdMonth='{1}' and WorkerID='{2}'";
            strSQL = string.Format(strSQL, strPrdYear, strPrdMonth, strWorkerID);
            dbcon.ExecuteSql(strSQL);

            strSQL = "Update rmWorkerOtherSalary Set Posted=0 Where PrdYear='{0}' and PrdMonth='{1}' and WorkerID='{2}'";
            strSQL = string.Format(strSQL, strPrdYear, strPrdMonth, strWorkerID);
            dbcon.ExecuteSql(strSQL);
            return true;
        }

        public bool UnConfirmSalary(DBAccess dbcon, string strRecID)
        {
            string strSQL="";
            DataRow drRow = dbcon.GetDataFirstRow("select * from rmWorkerSalary where RecID='"+ strRecID +"'");
            if (drRow != null)
            {
                strSQL = "Update rmWorkerSTDSalary Set Posted=0 Where PrdYear='{0}' and PrdMonth='{1}' and WorkerID='{2}'";
                strSQL = string.Format(strSQL, drRow["PrdYear"], drRow["PrdMonth"], drRow["WorkerID"]);
                dbcon.ExecuteSql(strSQL);

                strSQL = "Update rmWorkerOtherSalary Set Posted=0 Where PrdYear='{0}' and PrdMonth='{1}' and WorkerID='{2}'";
                strSQL = string.Format(strSQL, drRow["PrdYear"], drRow["PrdMonth"], drRow["WorkerID"]);
                dbcon.ExecuteSql(strSQL); 

                DataFunctions.AddLog(dbcon, "反审核" + CvtFunc.ToString(drRow["WorkerID"]) + CvtFunc.ToString(drRow["PrdYear"]) + "/" + CvtFunc.ToString(drRow["PrdMonth"]), "RM", "");
            }

            strSQL="Delete From rmWorkerSalary Where RecID='"+ strRecID +"'";
            dbcon.ExecuteSql(strSQL);

            return true;
        }

        //public bool UpdateDetail(DBAccess dbcon , string strRecID,DataRow drRow )
        //{
        //    string strSQL = "";
        //    strSQL = "Update rmWorkerSalary Set Remark";
        //}
    }
}
